package com.wl.cloud.core.utils;

import com.wl.cloud.core.dto.RestResultDTO;
import com.wl.cloud.core.exception.CustomException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

/**
 * @author: wanglin
 * @date: 2023-07-12 周三
 * @Version: 1.0
 * @Description:
 */
public class RestResultUtil {
    private static final Logger logger = LoggerFactory.getLogger(RestResultUtil.class);
    private static final String ERROR_MESSAGE_PREFIX = "服务调用失败！";

    public static RestResultDTO handleSuccess() {
        return handleSuccess(null, null);
    }

    public static RestResultDTO<String> handleSuccess(String message) {
        return handleSuccess(null, message);
    }

    public static <T> RestResultDTO<T> handleSuccess(T data) {
        return handleSuccess(data, null);
    }

    public static <T> RestResultDTO<T> handleSuccess(T data, String message) {
        message = StringUtils.isNotBlank(message) ? message : "成功";
        return RestResultDTO.newSuccess(data, message);
    }

    public static RestResultDTO<Exception> handleException(Exception e, String message) {
        if (Objects.isNull(e)) {
            RestResultDTO.newFail(message, "RestResultUtil.handleException的Exception类型参数不能为空");
        }

        if (e instanceof IllegalArgumentException || e instanceof IllegalStateException) {
            // 断言校验异常，日志记录为warn级别
            logger.warn(message, e);
            message = e.getMessage();
        } else if (e instanceof NullPointerException) {
            // 空指针异常，日志记录为error级别
            logger.error(message, e);
            // 空指针异常，堆栈打印
            e.printStackTrace();
            message = e.getMessage();
        } else if (e instanceof CustomException) {
            // 服务调用异常，日志记录为error级别
            logger.error(message, e);
            message = e.getMessage();
        } else {
            logger.error(message, e);
        }

        message = StringUtils.isNotBlank(message) ? message : "操作失败";

        return RestResultDTO.newFail(message, e.getClass().getName() + ":" + e.getMessage());
    }

    public static void handleRestResult(RestResultDTO restResultDto, String errorMessagePrefix) {
        if (Objects.isNull(restResultDto)) {
            String prefix = StringUtils.isNotBlank(errorMessagePrefix) ? errorMessagePrefix : ERROR_MESSAGE_PREFIX;
            throw new CustomException(prefix + "没有返回值");
        }

        if (!Objects.equals(restResultDto.getResult(), RestResultDTO.RESULT_SUCCESS)) {
            String prefix = StringUtils.isNotBlank(errorMessagePrefix) ? errorMessagePrefix : ERROR_MESSAGE_PREFIX;
            String msg = StringUtils.isNotBlank(restResultDto.getMsg()) ? restResultDto.getMsg() : StringUtils.EMPTY;
            String exception = StringUtils.isNotBlank(restResultDto.getException()) ? restResultDto.getException() : StringUtils.EMPTY;
            throw new CustomException(prefix + (StringUtils.isNotBlank(msg) ? msg : exception));
        }
    }
}
